<%@page import="systems.AdsSystem"%>
<%@page import="java.util.TreeMap"%>
<%@page import="entities.MetaSeo"%>
<%@page import="helpers.StringHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="helpers.DateHelper"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="helpers.IdUniqueHelper"%>
<%@page import="models.QuestionModel"%>
<%@page import="com.google.gson.Gson"%>
<%@page import="entities.Answer"%>
<%@page import="entities.Category"%>
<%@page import="java.util.Map"%>
<%@page import="entities.Author"%>
<%@page import="entities.Question"%>
<%@page import="java.util.List"%>
<%
Question question = (Question) request.getAttribute("question");
Map<String, Author> authors = (Map<String, Author>) request.getAttribute("authors");
Category category = (Category) request.getAttribute("category");
List<Answer> answers = (List<Answer>) request.getAttribute("answers");
Author author = null;
MetaSeo metaseo = (MetaSeo) request.getAttribute("metaseo");

//Set content for Seo
Map<String, String> replates = new TreeMap<String, String>();
replates.put("__SERVER_NAME__", UrlHelper.getDomainServer(request));
replates.put("__SERVER_URL__", UrlHelper.getServerUrl(request));
replates.put("__QUESTION_ID__", question.getId());
replates.put("__QUESTION_TITLE__", question.getTitle());
replates.put("__QUESTION_CONTENT__", StringHelper.html2text(question.getContent()));

String title = StringHelper.replateContent(metaseo.getTitle(), replates);
String description = StringHelper.replateContent(metaseo.getDescription(), replates);
String keywords = StringHelper.replateContent(metaseo.getKeywords(), replates);
String url = StringHelper.replateContent(metaseo.getOgurl(), replates);
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/wpanswer/layouts/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>

		<div class="row">
			<!-- Content BEGIN -->
			<div class="span8 main-content">
				<div class="content">
					<h1 class="title"><%=question.getTitle() %></h1>
					<div class="questionmeta">
						<%
						if (!question.getAuthor().isEmpty()) {
							author = authors.get(question.getAuthor());
							if (author != null) {
						%>
						<span>
							Asked by: 
							<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>"><%=author.getName() %></a>
						</span>
						<% 	} else { %>
						<span>
							Asked by: 
							<%=question.getAuthorName() %>
						</span>	
						<%  }
						} %>
						<% if (category != null) { %>
						<span>
							<a rel="tag" href="/category/<%=category.getAlias()%>-<%=category.getId() %>"><%=category.getName() %></a><br>
						</span>
						<% } %>
						
					</div>
					<%=question.getContent() %>
				</div>
				
				<!-- comments -->
				<div id="comments">
					<div class="comment-title">
						<div class="questionnumber">
							<p><a><%=answers.size() %></a></p>
						</div>
						<h2 class="commentheading">Answers</h2>
						<div class="clear"></div>
					</div>
					
					<div id="list-comments">
						<%
						for (Answer answer : answers) {
							author = authors.get(answer.getAuthor());
						%>
						<comment>
							<div class="comment-author vcard">
								<% if (author != null) { %>
								<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>">
									<img class="avatar" src="<%=author != null? author.getAvatar() : "" %>" width="48" height="48">
								</a>
								<% } else { %>
								<img class="avatar" src="<%=answer.getAuthorAvatar() %>" width="48" height="48">
								<% } %>
								<% if (author != null) { %>
								<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>"><%=author != null? author.getName() : "" %></a>
								<% } else { %>
								<%=answer.getAuthorName() %>
								<% } %>
							</div>
							
							<div class="wpanswerstuff">
						        <ul>
						        	<li><%=DateHelper.getStringFromDate(answer.getCreated(), "MMM dd, yyyy") %></li>
						        </ul>
						    </div>
						    
						    <div class="comment-body">
						    	<p><%=answer.getContent() %></p>
							</div>
							
							<div class="clear"></div>
						</comment>
						<% } %>
					</div>
				</div>
				<!-- comments end -->
				
				<!-- Same Question -->
				<% if (category != null) { %>
				<div id="other_question">
					<h3 class="title">Other Question in <%=category.getName() %></h3>
					<%
					if (!question.getLinks().isEmpty()) {
					%>
					<ul>
						<%
							QuestionModel questionModel = QuestionModel.getInstance();
							Map<String, String> links = (Map<String, String>) new Gson().fromJson(question.getLinks(), Map.class);
							for (Map.Entry<String, String> entry : links.entrySet()) {
								//Check key question exist
								url = entry.getValue();
								
						%>
							<li><a href="/question/<%=URLEncoder.encode(entry.getValue()) %> "><%=entry.getKey() %></a></li>
						<% } %>
					</ul>
					<% } %>
				</div>
				<% } %>
				<!-- end Same Question  -->
			</div>
			<!-- Content END -->
			
			<!-- slidebar BEGIN -->
			<jsp:include page="/templates/wpanswer/elements/right.jsp" />
			<!-- slidebar END -->
		</div>
<jsp:include page="/templates/wpanswer/layouts/footer.jsp" />